<template>
	<view style="padding-top: 20px;">
		<view class="list" v-if="addresses.length > 0">
			<view v-for="(item,index) in addresses" :key="index"
				style="margin:10px;padding:10px;background-color: #F3F3F3;border-radius: 5px;">
				<u-row>
					<u-col span="11">
						<view v-if="fromMine">
							<view style="font-weight: bold;">{{item.address}}</view>
							<view style="font-size:12px;padding-top:5px;">{{item.choosedAdd.address}}{{item.room}}
							</view>
							<view>
								<text style="font-size:11px;color:#909399;">{{item.name}}{{item.gender}}</text>
								<text style="font-size:11px;color:#909399;padding-left:10px;">{{item.phone}}</text>
							</view>
						</view>
						<view v-else @click="chooseAddress(index)">
							<view style="font-weight: bold;">{{item.address}}</view>
							<view style="font-size:12px;padding-top:5px;">{{item.choosedAdd.address}}{{item.room}}
							</view>
							<view>
								<text style="font-size:11px;color:#909399;">{{item.name}}{{item.gender}}</text>
								<text style="font-size:11px;color:#909399;padding-left:10px;">{{item.phone}}</text>
							</view>
						</view>
					</u-col>
					<u-col span="1">
						<u-icon name="trash-fill" color="#909399" size="32" @click="del(index)"></u-icon>
					</u-col>
				</u-row>
			</view>
		</view>
		<view v-else>
			<u-empty text="暂无地址记录" mode="address"></u-empty>
		</view>
		<view style="width:100%;position: fixed;bottom:10px;">
			<view style='padding:10px'>
				<u-button type="primary" shape="circle" @click="toUrl('/pages/mine/address-create')">新增地址</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addresses: [],
				fromMine: false,
				refundUrl: ''
			}
		},
		onShow() {
			let addresses = uni.getStorageSync('myAddresses')
			if (addresses != '') {
				this.addresses = addresses
			}
		},
		onLoad(options) {

			console.log(options)
			if (typeof(options.from) != 'undefined') {
				this.fromMine = true
			}

			if (typeof(options.refund) != 'undefined') {
				this.refundUrl = options.refund
			}
		},
		methods: {
			toUrl(page) {
				uni.navigateTo({
					url: page
				})
			},
			del(index) {
				uni.showModal({
					title: '',
					content: '是否删除该地址？',
					success: (data) => {
						if(data.confirm){
							this.addresses.splice(index, 1)
							uni.setStorageSync('myAddresses', this.addresses)
						}
					}
				})
			},
			chooseAddress(index) {
				let address = this.addresses[index]
				// todo 这里修改成根据选择的地址获取最近可配送门店列表，保存地址并且自动修改到该门店，如果没有的，就提示不能配送
				console.log('ll', address)
				const lat = address.choosedAdd.location.lat
				const lng = address.choosedAdd.location.lng
				// 看客户选择地址附近有无门店可以配送
				this.request.get(`api/stores`, {
					'@filter': `entity.getStatus() == 0 && math.locationDistance(entity.getLongitude(), entity.getLatitude(), ${lng}, ${lat}) < entity.getShippingRange()`
				}).then((res) => {
					console.log('lo', res.data)
					if (res.data.length == 0) {
						uni.showModal({
							title: '',
							content: '该区域配送即将开启',
							showCancel: false,
							confirmText: '好的'
						})
						return
					} else {
						let date = new Date()
						let year = date.getFullYear()
						let month = date.getMonth() + 1
						let day = date.getDate()
						let sureAddress = {
							date: `${year}-${month}-${day}`,
							address: address
						}
						uni.setStorageSync('sureAddress', sureAddress)
						uni.setStorageSync('store', res.data[0].id)
						if (this.refundUrl == '') {
							uni.navigateTo({
								url: '/pages/category/category'
							})
						} else {
							uni.switchTab({
								url: this.refundUrl
							})
						}
					}
				})


			}
		}
	}
</script>

<style>
	.time {
		color: #909399;
		font-size: 13px;
	}

	.add {
		color: #18B566;
		font-weight: bold;
		font-size: 16px;
	}

	.dev {
		color: #909399;
		font-weight: bold;
		font-size: 16px;
	}

	.wrap {
		padding: 10px 0px
	}
</style>